package project

import (
	"context"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/dto"
	projectDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/dto/project"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/opensearch"
	common2 "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/repository/common"
	commonPb "git.mycaigou.com/gfyx/micro-gfyx-mp-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"strings"
)

type SearchProjectService struct {
	searchProjectRepo SearchProjectRepositoryIface
	optionRepo        common2.OptionRepositoryIface
}

func NewSearchProject(
	searchProjectRepo SearchProjectRepositoryIface,
	optionRepo common2.OptionRepositoryIface,
) SearchProjectServiceIface {
	return &SearchProjectService{
		searchProjectRepo: searchProjectRepo,
		optionRepo:        optionRepo,
	}
}

func (s *SearchProjectService) CommonSearch(ctx context.Context, path string, params map[string]interface{}) (interface{}, error) {
	return s.searchProjectRepo.CommonSearch(ctx, path, params)
}

/******   分割线  ***************/
func (s *SearchProjectService) MohurdProjectList(ctx context.Context, request *projectDto.MohurdProjectListRequest) (interface{}, error) {
	opRequest := &opensearch.MohurdProjectListRequest{
		Keyword:     request.Keyword,
		CompanyType: strings.Join(request.CompanyType, ","),
		Page:        request.Page,
		PageSize:    request.PageSize,
	}
	if len(request.CityCode) == 0 && len(request.ProvinceCode) == 0 {
		//如果用户没有输入省市地区，默认带入用户有权益的省市
		resp, err := s.optionRepo.RadarAreaOption(ctx, &commonPb.GetRadarAreaOptionRequest{})
		if err != nil {
			return nil, err
		}
		if resp != nil {
			request.AreaCode = []string{}
			for _, option := range resp.Options {
				request.AreaCode = append(request.AreaCode, option.AreaCode)
			}
			opRequest.Area = strings.Join(request.AreaCode, ",")
		}
	} else {
		if len(request.CityCode) != 0 {
			opRequest.Area = strings.Join(request.CityCode, ",")
		}
		if len(request.ProvinceCode) != 0 {
			if opRequest.Area != "" {
				opRequest.Area += ","
			}
			opRequest.Area += strings.Join(request.ProvinceCode, ",")
		}
	}

	if len(request.TagsSet) > 0 {
		for _, v := range request.TagsSet {
			switch v {
			case "developer_top_100_label":
				opRequest.DeveloperTop100Label = "地产百强"
			case "new_decorate_expand_label":
				opRequest.NewDecorateExpandLabel = "新精装客群"
			case "new_land_usage_expand_label":
				opRequest.NewLandUsageExpandLabel = "新住宅客群"
			case "new_area_expand_label":
				opRequest.NewAreaExpandLabel = "新区域拓展"
			case "price_gt_avg_label":
				opRequest.PriceGtAvgLabel = "高端项目客群"
			default:

			}
		}
	}

	return s.searchProjectRepo.MohurdProjectList(ctx, opRequest)
}

func (s *SearchProjectService) ProjectDetail(ctx context.Context, dto *dto.CommonIdRequest) (interface{}, error) {
	return s.searchProjectRepo.ProjectDetail(ctx, dto)
}

func (s *SearchProjectService) AboutCompany(ctx context.Context, dto *projectDto.ProjectDetailRequest) (interface{}, error) {
	return s.searchProjectRepo.AboutCompany(ctx, dto)
}

func (s *SearchProjectService) BiddingList(ctx context.Context, dto *projectDto.ProjectDetailRequest) (interface{}, error) {
	return s.searchProjectRepo.BiddingList(ctx, dto)
}

func (s *SearchProjectService) Contract(ctx context.Context, dto *projectDto.ProjectDetailRequest) (interface{}, error) {
	return s.searchProjectRepo.Contract(ctx, dto)
}

func (s *SearchProjectService) Construction(ctx context.Context, dto *projectDto.ProjectDetailRequest) (interface{}, error) {
	return s.searchProjectRepo.Construction(ctx, dto)
}

func (s *SearchProjectService) License(ctx context.Context, dto *projectDto.ProjectDetailRequest) (interface{}, error) {
	return s.searchProjectRepo.License(ctx, dto)
}

func (s *SearchProjectService) CompletionRecord(ctx context.Context, dto *projectDto.ProjectDetailRequest) (interface{}, error) {
	return s.searchProjectRepo.CompletionRecord(ctx, dto)
}
